package scr 
{
	import flash.events.Event;
	import ru.gosoplata.model.TmpModel;
	import ru.gosoplata.view.ChooseProviderMediator;
	import ru.gosoplata.view.IChooseProviderView;
	import ru.gosoplata.view.FooterView;
	import uicontrols.ButtonBase;
	import uicontrols.ButtonProvider;
	import uicontrols.ButtonSearch;
	/**
	 * ...
	 * @author a.orleansky
	 */
	public class ScreenChooseProvider extends ScreenBase implements IChooseProviderView, IScreen
	{
		private var btns:Array = new Array();
		private var pics_loaded:Number=0;
		private var btn_search:ButtonSearch;
		
		private var _footer:FooterView = new FooterView();
		private var _mediator:ChooseProviderMediator;
		
		public function ScreenChooseProvider() 
		{
			super("choose_provider");
			_mediator = new ChooseProviderMediator(this);
		}
		
		override protected function init(e:Event):void 
		{
			super.init(e);
			
			addChild(_footer);
			
			btn_search = new ButtonSearch();
			btn_search.x = 30;
			btn_search.y = 190;
			btn_search.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			addChild(btn_search);
			
			var cnt:Number = 0;
			for (var ii:Number = 0; ii < 20; ii++ ) 
			{
				cnt++;
				var btn:ButtonProvider = new ButtonProvider("prov");
				btn.x = 30 + 140 * ((cnt-1) % 5);
				btn.y = 190 + 100 * (Math.floor((cnt-1) / 5));
				btn.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressPrv);
				btn.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
				pics_loaded++;
				btn.visible = false;
				btns.push(btn);
				addChild(btn);
				
			}
			hideBtns();
			
			loadBanner(TmpModel.IMG_FOLDER+"/sotovie_operatori/images/reklama_title.png");
		}		
		
		private function pressPrv(e:Event):void 
		{
			_mediator.onProviderSelected((e.target as ButtonProvider).name);
		}
		
		private function allPicLoaded(e:Event):void 
		{
			pics_loaded--;
			if (pics_loaded == 0) 
			{
				for (var ii:Number = 0; ii < 20; ii++ ) {
					var btn:ButtonProvider = (btns[ii] as ButtonProvider);
					btn.x = 30 + (btn.width +65)* ((ii) % 5);
					btn.y = 190 + (btn.height+16) * (Math.floor((ii) / 5));
				}
			}
		}
		
		private function hideBtns():void 
		{
			for (var ii:Number = 0; ii < 20; ii++ ) {
				var btn:ButtonProvider = (btns[ii] as ButtonProvider);
				btn.visible = false;
			}
		}
		
		override public function reDraw():void 
		{ 
			_footer.reDraw();
			_mediator.reDraw();
		}	
		
		public function showProviders(providers:XMLList):void 
		{ 
			hideBtns();
			
			var l:uint = Math.min(providers.length(), 19);//TODO: механизм показа больше чем 20 провайдеров не работает у Антона - мне же нужен нормальный UI
			//TmpModel.debugOutput.add("ScreenChooseProvider reDraw length:" + providers.length() + "/" + l);
			for (var ii:Number = 0; ii < l; ii++ ) 
			{
				var btn:ButtonProvider = btns[ii+1] as ButtonProvider;
				btn.ph_provider2 = providers[ii];
				btn.visible = true;
				//TmpModel.debugOutput.add("ScreenChooseProvider :" + ii + " " + (providers[ii] as XML).toXMLString());
			}
		}
		
		//TODO:case "back": показ предыдущих 20 провайдеров или возврат - не работает - да и не правельно использовать эти кнопки для промотки провайдеров!!!
		private function pressBtn(e:Event):void 
		{ 
			_mediator.onSearchStart();
		}
	}
}